<?php

abstract class Module
{
    private $moduleName;
    private $modulePath;
    private $QXC;
    
    function __construct($name = '')
    {
        if (! empty($name) && is_string($name))
        {
            $this->moduleName = strtolower($name);
        }
    }

    public static function Load($name = '')
    {        
        if (! empty($name))
        {            
            return self::loadModule($name);
        }        
    }

    // Private Methods
    private static function loadModule($name)
    {
        $QXC = QXC();
        $moduleName = strtolower($name);
        $customModules = $QXC->Config->modules;

        // First try to load controller from directories enumerated in main config
        if (is_array($customModules))
        {            
            foreach ($customModules as $val)
            {                
                $tempPath = ($val{0} == "/") ? $val : ROOT_DIR . "/{$val}";

                if (file_exists($path = $tempPath . "/{$moduleName}/index." . CORE_PHP_EXT))
                {
                    include_once ($path);

                    $object = new $name($QXC);
                    $object->QXC = $QXC;                    
                    $object->PATH = dirname($path);

                    return $object;
                }
            }
        }

        return false;
    }

    // Magic Methods
    protected function __get($name)
    {
        $QXC = QXC();

        return $QXC->{$name};
    }
}